//
//  FirstViewController.m
//  Molian
//
//  Created by xiaocankeji on 15/8/27.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "FirstViewController.h"
#import "MLUserDataViewController.h"//用户资料
#import "SweetheartView.h"
#import "UserGuidanceViewController.h"
#import "ChatViewController.h"
#import "SRRefreshView.h" //下拉刷新
#import "GifView.h"
#import "ThirdpartyPhoneViewController.h"

#define kChiefViewHeight DeviceWidth/3*2
#define kButtonTag 199
#define kSweetheartsViewHeight  58


@interface FirstViewController ()<SRRefreshDelegate,UIScrollViewDelegate,SweetheartViewDelegate,UITableViewDataSource, UITableViewDelegate>

//下拉刷新
@property (strong, nonatomic) SRRefreshView *slimeView;
//视图列表
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIImageView *moveTriangle;//三角图片
@property (nonatomic)NSInteger presentSweetheartCount;//当前恋人数量
@property (nonatomic, strong)UILabel *sweetheartSpecialLabel;
@property (nonatomic, strong)GifView *sweetheartSpecialGifView;
@property (nonatomic, strong)NSString *chatObjectId;
//@property (nonatomic)BOOL isLoadData;//是否已经加载过数据

@end

@implementation FirstViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (![[EaseMob sharedInstance].chatManager isConnected]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginEaseMob" object:self userInfo:nil];
        }
    });
    
    [[ShareData shareData].firstView messageNumberChange];
    [[ShareData shareData].secondView messageNumberChange];
    [[ShareData shareData].thirdView messageNumberChange];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        MLUser *user = [MLUser currentUser];
       
        if (user&&user.isUserInited) {
            if ((user.notifyPhoneNum == nil&&user.mobilePhoneNumber == nil)||(user.mobilePhoneNumber&&user.mobilePhoneNumber.length == 0)||(user.mobilePhoneNumber == nil&&user.notifyPhoneNum.length == 0)) {
                ThirdpartyPhoneViewController *thirdPVC = [[ThirdpartyPhoneViewController alloc] init];
                thirdPVC.noRegister = YES;
                [Utils pushTabbarHidden:thirdPVC Selfvc:self];
            }else{
                
                if (user.notifyPhoneNum == nil||user.notifyPhoneNum.length==0) {
                    user.notifyPhoneNum = user.mobilePhoneNumber;
                    [user saveInBackground];
                }
            }
        }
    });
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
//闪屏页面的加载
- (void)splashScreenView
{
    
    UIView *windowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)];
    windowView.backgroundColor = [UIColor whiteColor];
    [[UIApplication sharedApplication].delegate.window addSubview:windowView];
    
    UIImageView *qidong = [[UIImageView alloc]initWithFrame:CGRectMake((DeviceWidth-130)/2, 100, 130, 130)];
    qidong.image = [UIImage imageNamed:@"start_image"];
    [qidong setContentMode:UIViewContentModeScaleAspectFill];
    [windowView addSubview:qidong];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake((DeviceWidth-17)/2, 144, 17, 5)];
    [image setContentMode:UIViewContentModeScaleAspectFill];
    [windowView addSubview:image];
    
    UIImageView *pocktsweet = [[UIImageView alloc]initWithFrame:CGRectMake((DeviceWidth-93)/2, DeviceHeight-41.5, 93, 12)];
    pocktsweet.image = [UIImage imageNamed:@"pocktsweet"];
    [pocktsweet setContentMode:UIViewContentModeScaleAspectFill];
    [windowView addSubview:pocktsweet];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.775 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSMutableArray *gifArray = [NSMutableArray array];
        for (int i = 1 ; i < 10; i ++) {
            [gifArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"eye%d",i]]];
        }
        image.animationImages = gifArray; //动画图片数组
        image.animationDuration = 0.45; //执行一次完整动画所需的时长
        image.animationRepeatCount = 1;  //动画重复次数
        [image startAnimating];
        
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [Utils Hidden:windowView Bool:NO];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [windowView removeFromSuperview];
        });
    });
}

- (void)clickSweetheartSpecial
{
    if (self.sweetheartSpecial.alpha == 1) {
        [Utils Hidden:self.sweetheartSpecial Bool:NO];
        [self.sweetheartSpecialGifView stopGif];
    }else{
        [Utils Hidden:self.sweetheartSpecial Bool:YES];
    }
}
- (UIView *)sweetheartSpecial
{
    if (_sweetheartSpecial == nil) {
        
        _sweetheartSpecial = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)];
        _sweetheartSpecial.backgroundColor = RGBA(1, 1, 1, 0.3);
        
        [[UIApplication sharedApplication].delegate.window addSubview:_sweetheartSpecial];
        _sweetheartSpecial.alpha = 0;
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickSweetheartSpecial)];
        [_sweetheartSpecial addGestureRecognizer:tapGR];
        
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(DeviceWidth/2-127.5, DeviceHeight/2-127.5, 255, 255)];
        whiteView.backgroundColor = [UIColor whiteColor];
        whiteView.layer.masksToBounds = YES;
        whiteView.layer.cornerRadius = 5;
        [_sweetheartSpecial addSubview:whiteView];
        
        self.sweetheartSpecialLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 20, whiteView.frame.size.width-10, 20)];
        self.sweetheartSpecialLabel.textColor = RGBA(14, 16, 18, 1);
        self.sweetheartSpecialLabel.font = kFont(16);
        self.sweetheartSpecialLabel.textAlignment = NSTextAlignmentCenter;
        self.sweetheartSpecialLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
        [whiteView addSubview:self.sweetheartSpecialLabel];
        
        self.sweetheartSpecialGifView = [[GifView alloc] initWithFrame:CGRectMake(whiteView.frame.size.width/2-80, 50, 160, 160)];
        [whiteView addSubview:self.sweetheartSpecialGifView];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, whiteView.frame.size.height - 45, whiteView.frame.size.width, 0.5)];
        line.backgroundColor = RGBA(1, 1, 1, 0.1);
        [whiteView addSubview:line];
        UIView *otherLine = [[UIView alloc] initWithFrame:CGRectMake(whiteView.frame.size.width/2.0-0.25, whiteView.frame.size.height - 45, 0.5, 45)];
        otherLine.backgroundColor = RGBA(1, 1, 1, 0.1);
        [whiteView addSubview:otherLine];
        
        UIButton *leftbt = [UIButton buttonWithType:UIButtonTypeCustom];
        leftbt.frame = CGRectMake(0, whiteView.frame.size.height - 46, whiteView.frame.size.width/2, 44);
        [leftbt setTitle:@"知道了" forState:UIControlStateNormal];
        [leftbt setTitleColor:RGBA(14, 16, 18, 1) forState:UIControlStateNormal];
        leftbt.titleLabel.font = kFont(15);
        [leftbt addTarget:self action:@selector(clickSweetheartSpecial) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:leftbt];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(whiteView.frame.size.width/2, whiteView.frame.size.height - 46, whiteView.frame.size.width/2, 44);
        [bt setTitle:@"去聊聊" forState:UIControlStateNormal];
        [bt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
        bt.titleLabel.font = kFont(15);
        [bt addTarget:self action:@selector(goSweetheartChat) forControlEvents:UIControlEventTouchUpInside];
        [whiteView addSubview:bt];
        
        
    }
    return _sweetheartSpecial;
}
//获取当前窗口的vc
-(UIViewController *)getCurrentRootViewController {
    
    
    UIViewController *result;
    
    
    // Try to find the root view controller programmically
    
    
    // Find the top window (that is not an alert view or other window)
    
    
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    
    
    if (topWindow.windowLevel != UIWindowLevelNormal)
        
        
    {
        
        
        NSArray *windows = [[UIApplication sharedApplication] windows];
        
        
        for(topWindow in windows)
            
            
        {
            
            
            if (topWindow.windowLevel == UIWindowLevelNormal)
                
                
                break;
            
            
        }
        
        
    }
    
    
    UIView *rootView = [[topWindow subviews] objectAtIndex:0];
    
    
    id nextResponder = [rootView nextResponder];
    
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        
        
        result = nextResponder;
    
    
    else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
        
        
        result = topWindow.rootViewController;
    
    
    else
        
        
        NSAssert(NO, @"ShareKit: Could not find a root view controller.  You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
    
    
    return result;    
    
    
}
//去和恋人聊天
- (void)goSweetheartChat
{
    [Utils Hidden:self.sweetheartSpecial Bool:NO];
    [self.sweetheartSpecialGifView stopGif];
    
    UIViewController *current = [self getCurrentRootViewController];
    if(current){
         [current.navigationController popToRootViewControllerAnimated:YES];
    }
   
    self.tabBarController.selectedIndex = 0;
    
    
    if ([ShareData shareData].firstView.sweetheart) {
        if ([[ShareData shareData].firstView.sweetheartUser.objectId isEqualToString:self.chatObjectId]) {
            [self pushChatViewControllerUser:[ShareData shareData].firstView.sweetheartUser isPay:NO];
            return;
        }
    }
    if ([ShareData shareData].secondView.sweetheart) {
        if ([[ShareData shareData].secondView.sweetheartUser.objectId isEqualToString:self.chatObjectId]) {
            [self pushChatViewControllerUser:[ShareData shareData].secondView.sweetheartUser isPay:NO];
            return;
        }
    }
    if ([ShareData shareData].thirdView.sweetheart) {
        if ([[ShareData shareData].thirdView.sweetheartUser.objectId isEqualToString:self.chatObjectId]) {
            [self pushChatViewControllerUser:[ShareData shareData].thirdView.sweetheartUser isPay:NO];
            return;
        }
    }
        AVQuery *q = [MLUser query];
        [q whereKey:@"objectId" equalTo:self.chatObjectId];
        [HUD setOnViewWithTitle:@"加载中" animated:YES];
    
        [q getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
           [HUD hideFromViewAnimated:YES];
            if (error == nil) {
                MLUser *u = (MLUser *)object;
                if (u) {
                   [self pushChatViewControllerUser:u isPay:NO];
                    MLUser *user = [MLUser currentUser];
                    if (user == nil) {
                        return;
                    }
                    [self queryHandleSweetheartLoad:YES one:YES];
                }else{
                   [HUD Prompt:@"请重新登录"];
                }
            }else{
                [HUD Prompt:@"网络错误"];
            }
            
        }];
}
-(void)sweetheartMessage:(NSNotification *)notification
{
    NSDictionary *dic=[notification userInfo];
    EMMessage *message = [dic objectForKey:@"message"];
    
    if([message.ext objectForKey:LITTLE_ACTION_TYPE_MOMODA]&&[[message.ext objectForKey:LITTLE_ACTION_TYPE_MOMODA]isEqualToString:LITTLE_ACTION_TYPE_MOMODA_VALUE]){
        NSString *nickname = [message.ext objectForKey:@"nickname"];
        [self showsweetheartSpecialGifType:LITTLE_ACTION_TYPE_MOMODA_VALUE nickname:nickname chatObjectId:message.from];
        
    }else if([message.ext objectForKey:LITTLE_ACTION_TYPE_CHUO]&&[[message.ext objectForKey:LITTLE_ACTION_TYPE_CHUO]isEqualToString:LITTLE_ACTION_TYPE_CHUO_VALUE]){
        NSString *nickname = [message.ext objectForKey:@"nickname"];
        
        [self showsweetheartSpecialGifType:LITTLE_ACTION_TYPE_CHUO_VALUE nickname:nickname chatObjectId:message.from];
        
    }
}
- (void)showsweetheartSpecialGifType:(NSString *)type nickname:(NSString *)nickname chatObjectId:(NSString *)chatObjectId
{
    self.chatObjectId = chatObjectId;
    NSString *gifPath ;
    [Utils Hidden:self.sweetheartSpecial Bool:YES];
    if ([type isEqualToString:LITTLE_ACTION_TYPE_MOMODA_VALUE]) {
        //么么哒
        gifPath = [[NSBundle mainBundle] pathForResource:@"么么哒_gif" ofType:@"gif"];
        self.sweetheartSpecialLabel.text = [NSString stringWithFormat:@"%@对你么么哒",nickname];
        [self.sweetheartSpecialGifView beginGifFilePath:gifPath];
    }else if([type isEqualToString:LITTLE_ACTION_TYPE_CHUO_VALUE]){
        //戳一下
        gifPath = [[NSBundle mainBundle] pathForResource:@"戳一下_gif" ofType:@"gif"];
        self.sweetheartSpecialLabel.text = [NSString stringWithFormat:@"%@戳了你一下",nickname];
        [self.sweetheartSpecialGifView beginGifFilePath:gifPath];
    }
    
}
#pragma mark  SweetheartView
- (void)pushUserDataViewControllerUser:(MLUser *)user;
{
    MLUserDataViewController *userInfoVC = [[MLUserDataViewController alloc]init];
    userInfoVC.userData = user;
    userInfoVC.redact = YES;
    //    userInfoVC.likeUsers = self.likeUsers;
    [Utils pushTabbarHidden:userInfoVC Selfvc:self];
}
- (void)pushChatViewControllerUser:(MLUser *)user  isPay:(BOOL)isPay
{
    ChatViewController * chatController = [[ChatViewController alloc] initWithConversationChatter:user.objectId conversationType:eConversationTypeChat];
    chatController.chatUser = user;
   
    chatController.title = user.nickname;
    chatController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chatController animated:YES];
}
- (void)changeTableViewHeightBOOL:(BOOL)is
{
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (_slimeView) {
        [_slimeView scrollViewDidEndDraging];
    }
}
//下拉
- (void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView
{
    MLUser *user = [MLUser currentUser];
    if (user == nil) {
        //加载停止
        [self.slimeView endRefresh];
        return;
    }
    
    if (user == nil) {
        return;
    }
    [self queryHandleSweetheartLoad:YES one:YES];
    
}

- (void)addScrollviewAndSweetheartsView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight-kMyTabBarHeight)style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   // self.tableView.delaysContentTouches = NO;
    self.tableView.scrollsToTop = YES;
    self.tableView.tableHeaderView = [[UIView alloc] init];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorColor = RGBA(225, 225, 225, 1);//分隔条颜色
    [self.view addSubview:self.tableView];
    
   
    // Do any additional setup after loading the view.
    
    _slimeView = [[SRRefreshView alloc] init];
    _slimeView.delegate = self;
    _slimeView.upInset = 0;
    _slimeView.slimeMissWhenGoingBack = YES;
    _slimeView.slime.bodyColor = APPCOLOR(1);
    _slimeView.slime.skinColor = [UIColor whiteColor];
    _slimeView.slime.lineWith = 1;
    _slimeView.slime.shadowBlur = 0;
    _slimeView.slime.shadowColor = [UIColor redColor];
    [self.tableView addSubview:self.slimeView];
    
    UIImageView *sweetheartsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, DeviceHeight-kSweetheartsViewHeight - kMyTabBarHeight, DeviceWidth, kSweetheartsViewHeight)];
    sweetheartsImageView.backgroundColor = [UIColor whiteColor];
    UIImage *img = [UIImage imageNamed:@"投影"];
    img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
    sweetheartsImageView.image = img;
    sweetheartsImageView.userInteractionEnabled = YES;
   // UISwipeGestureRecognizer *leftSwipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeGR:)];
    //[leftSwipeGR setDirection:UISwipeGestureRecognizerDirectionLeft];
    //[sweetheartsImageView addGestureRecognizer:leftSwipeGR];
    //UISwipeGestureRecognizer *rightSwipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeGR:)];
    //[rightSwipeGR setDirection:UISwipeGestureRecognizerDirectionRight];
    //[sweetheartsImageView addGestureRecognizer:rightSwipeGR];
    [self.tableView addSubview:sweetheartsImageView];
    
    
    self.moveTriangle = [[UIImageView alloc]initWithFrame:CGRectMake(DeviceWidth/2-32/2, 0, 32, 16)];
    self.presentSweetheartCount = 1;
    self.moveTriangle.image = [UIImage imageNamed:@"三角"];
    [sweetheartsImageView addSubview:self.moveTriangle];
    
      ShareData *shareData = [ShareData shareData];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    shareData.scrollView = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight-kMyTabBarHeight)];
   // shareData.scrollView.delaysContentTouches = NO;
    shareData.scrollView.alwaysBounceHorizontal = YES;
    shareData.scrollView.bounces = NO;
    shareData.scrollView.pagingEnabled = YES;
    shareData.scrollView.delegate = self;
    shareData.scrollView.showsHorizontalScrollIndicator = NO;
    shareData.scrollView.showsVerticalScrollIndicator = NO;
    shareData.scrollView.scrollsToTop = NO;
    [self.tableView addSubview:shareData.scrollView];
    [shareData.scrollView setContentSize:CGSizeMake(DeviceWidth, DeviceHeight - kMyTabBarHeight)];
    
  
    
    shareData.firstView =  [[SweetheartView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight - kMyTabBarHeight)];
    shareData.firstView.nameBt = [UIButton buttonWithType:UIButtonTypeCustom];
    shareData.firstView.nameBt.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    shareData.firstView.nameBt.frame = CGRectMake(0, 0 , DeviceWidth/3 , kSweetheartsViewHeight);
    // [bt setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
    [shareData.firstView.nameBt setTitleColor:RGBA(133, 133, 133, 1) forState:UIControlStateNormal];
   
    shareData.firstView.nameBt.tag = kButtonTag;
    shareData.firstView.nameBt.hidden = YES;
    //[shareData.firstView.nameBt addTarget:self action:@selector(clikcBt:) forControlEvents:UIControlEventTouchUpInside];
    [sweetheartsImageView addSubview: shareData.firstView.nameBt];
    [ShareData shareData].firstView.delegate = self;
    
    [shareData.scrollView addSubview:[ShareData shareData].firstView];
    
    [ShareData shareData].secondView = [[SweetheartView alloc] initWithFrame:CGRectMake(DeviceWidth, 0, DeviceWidth, DeviceHeight - kMyTabBarHeight)];
    shareData.secondView.nameBt = [UIButton buttonWithType:UIButtonTypeCustom];
    shareData.secondView.nameBt.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    shareData.secondView.nameBt.hidden = YES;
    shareData.secondView.nameBt.frame = CGRectMake(1*DeviceWidth/3, 0 , DeviceWidth/3 , kSweetheartsViewHeight);
    [shareData.secondView.nameBt setTitleColor:RGBA(133, 133, 133, 1) forState:UIControlStateNormal];
    shareData.secondView.nameBt.tag = kButtonTag+1;
   // [shareData.secondView.nameBt addTarget:self action:@selector(clikcBt:) forControlEvents:UIControlEventTouchUpInside];
    [sweetheartsImageView addSubview: shareData.secondView.nameBt];
    [ShareData shareData].secondView.delegate = self;
    [shareData.scrollView addSubview:[ShareData shareData].secondView];
    
    [ShareData shareData].thirdView = [[SweetheartView alloc] initWithFrame:CGRectMake(DeviceWidth *2, 0, DeviceWidth, DeviceHeight - kMyTabBarHeight)];
    shareData.thirdView.nameBt.hidden = YES;
    shareData.thirdView.nameBt.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    shareData.thirdView.nameBt = [UIButton buttonWithType:UIButtonTypeCustom];
    shareData.thirdView.nameBt.frame = CGRectMake(2*DeviceWidth/3, 0 , DeviceWidth/3 , kSweetheartsViewHeight);
    
    [shareData.thirdView.nameBt setTitleColor:RGBA(133, 133, 133, 1) forState:UIControlStateNormal];
    shareData.thirdView.nameBt.tag = kButtonTag+2;
   // [shareData.thirdView.nameBt addTarget:self action:@selector(clikcBt:) forControlEvents:UIControlEventTouchUpInside];
    [sweetheartsImageView addSubview: shareData.thirdView.nameBt];
    [ShareData shareData].thirdView.delegate = self;
    [shareData.scrollView addSubview:[ShareData shareData].thirdView];
    [shareData.scrollView setContentOffset:CGPointMake(0, 0)];
}


- (void)rightSwipeGR:(UISwipeGestureRecognizer *)swipeGR
{
    ShareData *shareData = [ShareData shareData];
    if (swipeGR.direction == UISwipeGestureRecognizerDirectionRight){
        if (shareData.scrollView.contentOffset.x == DeviceWidth) {
            [self changeScrollViewContentOffset:0];
        }else if (shareData.scrollView.contentOffset.x == DeviceWidth*2) {
            [self changeScrollViewContentOffset:1];
        }
    }
}
- (void)leftSwipeGR:(UISwipeGestureRecognizer *)swipeGR
{
    ShareData *shareData = [ShareData shareData];
    if(swipeGR.direction == UISwipeGestureRecognizerDirectionLeft){
        if (shareData.scrollView.contentOffset.x == 0) {
            [self changeScrollViewContentOffset:1];
        }else if (shareData.scrollView.contentOffset.x == DeviceWidth) {
            [self changeScrollViewContentOffset:2];
        }
        
    }
}
- (void)changeScrollViewContentOffset:(NSInteger)page
{
    
    NSInteger number = 0;
    if ([ShareData shareData].firstView.sweetheart) {
        number++;
    }
    if ([ShareData shareData].secondView.sweetheart) {
        number++;
    }
    if ([ShareData shareData].thirdView.sweetheart) {
        number++;
    }
    if (number == 0) {
        return;
    }
    
    UIButton *bt = (UIButton *)[self.view viewWithTag:kButtonTag];
    UIButton *bt1 = (UIButton *)[self.view viewWithTag:kButtonTag+1];
    UIButton *bt2 = (UIButton *)[self.view viewWithTag:kButtonTag+2];
    [bt setTitleColor:RGBA(133, 133, 133, 1) forState:UIControlStateNormal];
    [bt1 setTitleColor:RGBA(133, 133, 133, 1) forState:UIControlStateNormal];
    [bt2 setTitleColor:RGBA(133, 133, 133, 1) forState:UIControlStateNormal];
    
    UIButton *nBt = (UIButton *)[self.view viewWithTag:kButtonTag+page];
    [nBt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
     ShareData *shareData = [ShareData shareData];
    switch (page) {
        case 0:
            if([ShareData shareData].firstView.sweetheart){
                [shareData.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            }
            
            break;
        case 1:
            if([ShareData shareData].secondView.sweetheart){
                [shareData.scrollView setContentOffset:CGPointMake(DeviceWidth, 0) animated:YES];
            }
           
            break;
        case 2:
            if([ShareData shareData].thirdView.sweetheart){
                [shareData.scrollView setContentOffset:CGPointMake(DeviceWidth*2, 0) animated:YES];
            }
            break;
        default:
            break;
    }
    
}

//用户引导页
- (void)clickBt
{
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (void)clikcBt:(UIButton *)bt
{
    
    if(bt.titleLabel.text.length == 0){
        return;
    }
    [self changeScrollViewContentOffset:bt.tag-kButtonTag];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
   
    if ([Utils onePerform:@"appGuide"]) {
        UserGuidanceViewController *uGVC = [[UserGuidanceViewController alloc] initWithVC:self];
        //        controller.modalPresentationStyle = UIModalPresentationFormSheet;
        [self presentViewController:uGVC animated:NO completion:nil];
    }
    //
    AVQuery *q = [MLUser query];
    [q whereKey:@"username" equalTo:@"18515663609"];
    [q getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        MLUser *user = (MLUser *)object;
        AVFile *file = user.avatar;
        NSData *data = file.getData;
        NSLog(@"%@%@",user,data);
        
        
        
        
        
        
        
    }];
    
    
    
  //  [self splashScreenView];
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    [self addScrollviewAndSweetheartsView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sweetheartChange) name:@"sweetheartChange" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sweetheartMessage:) name:@"sweetheartMessage" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cmd:) name:@"cmd" object:nil];
    MLUser *user = [MLUser currentUser];
    if (user == nil) {
        return;
    }
    [self queryHandleSweetheartLoad:YES one:YES];
    // Do any additional setup after loading the view.
}
-(void)cmd:(NSNotification *)notification
{
    NSDictionary *dic=[notification userInfo];
    EMMessage *message = [dic objectForKey:@"message"];
    if (message.messageBodies&&message.messageBodies.count) {
        EMCommandMessageBody *body= message.messageBodies.lastObject;
        if (body.action&&[body.action isEqualToString:ACTION_NEW_UPDATE_PROFILE]) {
            [self changePortraitObjectId:message.from];
        }
    }
}
//恋人资料改变了
- (void)sweetheartDataChangeUser:(MLUser *)user
{
    ShareData *sharedata = [ShareData shareData];
    if (sharedata.firstView.sweetheart&&[sharedata.firstView.sweetheartUser.objectId isEqualToString:user.objectId]) {
        sharedata.firstView.sweetheartUser = user;
        [sharedata.firstView changeSweetheartData];
    }else if (sharedata.secondView.sweetheart&&[sharedata.secondView.sweetheartUser.objectId isEqualToString:user.objectId]) {
        sharedata.secondView.sweetheartUser = user;
        [sharedata.secondView changeSweetheartData];
    }else if (sharedata.thirdView.sweetheart&&[sharedata.thirdView.sweetheartUser.objectId isEqualToString:user.objectId]) {
        sharedata.thirdView.sweetheartUser = user;
        [sharedata.thirdView changeSweetheartData];
    }
}


//收到好友换头像,昵称等
- (void)changePortraitObjectId:(NSString *)objectId
{
    
    if (([ShareData shareData].friends&&[ShareData shareData].friends.count>0)||([Utils isSweetheartObjectId:objectId])) {
        AVQuery *q = [MLUser query];
        [q whereKey:@"objectId" equalTo:objectId];
        [q getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
            MLUser *n = (MLUser *)object;
            
            if ([Utils isSweetheartObjectId:objectId]) {
                //是恋人,修改昵称
                [self sweetheartDataChangeUser:n];
            }
            
            
            [[ShareData shareData].chatterUsers setObject:n forKey:n.objectId];
            [Utils saveChatUserDataNicknameUser:n];
            for (int i = 0; i<[ShareData shareData].friends.count; i++) {
                MLUser *current = [[ShareData shareData].friends objectAtIndex:i];
                if ([current.objectId isEqualToString:n.objectId]) {
                    [[ShareData shareData].friends removeObjectAtIndex:i];
                    [[ShareData shareData].friends addObject:n];
                    
                    //通知好友 我更改信息
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"loadMyFriendsData" object:self userInfo:nil];
                    break;
                }
            }
        }];
    }
}

//计算剩余时间
- (NSInteger)manageDateBeginDate:(NSDate *)beginDate presentDate:(NSTimeInterval)presentInterval buyMinute:(NSInteger )buyMinute
{
    NSTimeInterval begin = [beginDate timeIntervalSince1970];
    NSTimeInterval cha = (presentInterval - begin);
    NSTimeInterval buy = buyMinute * 60;
    return buy - cha;
}


//根据订单请求状态决定是否需要重新请求订单

//恋人关系改变,重新加载恋人关系(透传过来的不需要加载遮挡视图)
- (void)sweetheartChange
{
    MLUser *user = [MLUser currentUser];
    if (user == nil) {
        return;
    }
    [self queryHandleSweetheartLoad:NO one:YES];
}
- (void)changeTogetherLabelCenter
{
    ShareData *shareData = [ShareData shareData];
    if (shareData.firstView.sweetheart) {
        if ([shareData.firstView.togetherLabel.text hasPrefix:@"已经有"]) {
            shareData.firstView.togetherLabel.text = [NSString stringWithFormat:@"已经有%ld人找到了虚拟恋人",(long)[Utils getPocketloveNumber]];
        }
    }
    if (shareData.secondView.sweetheart) {
        if ([shareData.secondView.togetherLabel.text hasPrefix:@"已经有"]) {
            shareData.secondView.togetherLabel.text = [NSString stringWithFormat:@"已经有%ld人找到了虚拟恋人",(long)[Utils getPocketloveNumber]];
        }
    }
    if (shareData.thirdView.sweetheart) {
        if ([shareData.thirdView.togetherLabel.text hasPrefix:@"已经有"]) {
            shareData.thirdView.togetherLabel.text = [NSString stringWithFormat:@"已经有%ld人找到了虚拟恋人",(long)[Utils getPocketloveNumber]];
        }
    }
}
- (void)queryHandleSweetheartLoad:(BOOL)load one:(BOOL)one
{
    MLUser *user = [MLUser currentUser];
    if (user == nil) {
        return;
    }
   
    AVQuery *querySponor = [MLSweetheart query];
    [querySponor whereKey:@"sponsor" equalTo:[MLUser currentUser]];
    [querySponor includeKey:@"winner"];
    [querySponor includeKey:@"sponsor"];
    [querySponor includeKey:@"belongOrder"];
    //正在进行的恋爱关系
    [querySponor whereKey:@"status" equalTo:[NSNumber numberWithInteger:1]];
    AVQuery *queryWinner = [MLSweetheart query];
    [queryWinner whereKey:@"winner" equalTo:[MLUser currentUser]];
    [queryWinner includeKey:@"winner"];
    [queryWinner includeKey:@"sponsor"];
    [queryWinner includeKey:@"belongOrder"];
    [queryWinner whereKey:@"status" equalTo:[NSNumber numberWithInteger:1]];
    
    AVQuery *query = [AVQuery orQueryWithSubqueries:[NSArray arrayWithObjects:queryWinner,querySponor, nil]];
    [query includeKey:@"winner"];
    [query includeKey:@"sponsor"];
    [query includeKey:@"belongOrder"];
    if (load) {
        [HUD setOnViewWithTitle:@"加载中" animated:YES];
    }
    ShareData *shareData = [ShareData shareData];
    shareData.sweetheartType = sweetheartType_QUERY;
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        //加载停止
        [self.slimeView endRefresh];
        if(error){
            if ([[error localizedDescription] isEqualToString:@"no results matched the query"]) {
                [HUD hideFromViewAnimated:YES];
                 shareData.sweetheartType = sweetheartType_DONE;
                ShareData *shareData = [ShareData shareData];
                if (shareData.allSweetheartCount == 0 ) {
                    AVQuery *q = [MLSweetheart query];
                    [q countObjectsInBackgroundWithBlock:^(NSInteger number, NSError *error) {
                        if (!error) {
                            [Utils savePocketloveNumber:number];
                            [self changeTogetherLabelCenter];
                        }else{
                          
                        }
                    }];
                }
               [HUD Prompt:@"还没有恋人,赶快去找个恋人吧"];
            }else{
                 shareData.sweetheartType = sweetheartType_NO;
                
                if (one) {
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self queryHandleSweetheartLoad:NO one:NO];
                    });
                }else{
                    [HUD hideFromViewAnimated:YES];
                    [HUD Prompt:@"网络错误,下拉刷新重试"];
                }
                //
                
            }
        }else{
            
            
            [shareData.firstView logOut];
             [shareData.secondView logOut];
             [shareData.thirdView logOut];
            [shareData.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            [shareData.scrollView setContentSize:CGSizeMake(DeviceWidth, DeviceHeight - kMyTabBarHeight)];
           
            shareData.sweetheartType = sweetheartType_DONE;
            if (objects&&objects.count>0) {
                for (int i = 0; i<objects.count; i++) {
                    MLSweetheart *sweetheart = (MLSweetheart *)objects[i];
                    MLUser *user = [MLUser currentUser];
                    if ([sweetheart.sponsor.objectId isEqualToString:user.objectId]) {
                        //我是发起人
                        //判断
                        //MLUser *winner = (MLUser *)sweetheart.winner;
                        [self changeSweetheartViewCenterSweetheart:sweetheart];
                    }else{
                        MLUser *sponsor = (MLUser *)sweetheart.sponsor;
                         [self changeSweetheartViewCenterSweetheart:sweetheart];
                    }
                }
                
                
            }else{
                
                ShareData *shareData = [ShareData shareData];
                
                if (shareData.allSweetheartCount == 0 ) {
                    AVQuery *q = [MLSweetheart query];
                    [q countObjectsInBackgroundWithBlock:^(NSInteger number, NSError *error) {
                        if (!error) {
                            [Utils savePocketloveNumber:number];
                            [self changeTogetherLabelCenter];
                        }else{
                            
                        }
                    }];
                }
                
                 [HUD hideFromViewAnimated:YES];
                
               // [HUD Prompt:@"还没有恋人,赶快去找个恋人吧"];
            }
        }
    }];
}
- (NSInteger)manageDateBeginDate:(NSDate *)beginDate presentDate:(NSTimeInterval)presentInterval
{
    NSTimeInterval begin = [beginDate timeIntervalSince1970];
    NSTimeInterval cha = (presentInterval - begin);
    return cha;
}
// order必须有值 count也必须有
- (void)changeSweetheartViewCenterSweetheart:(MLSweetheart *)sweetheart
{
    NSString *name = @"";
    MLUser *taUser;
    if ([sweetheart.sponsor.objectId isEqualToString:[MLUser currentUser].objectId]) {
        taUser = (MLUser *)sweetheart.winner;
        name = taUser.nickname;
        //我是发起人
    }else{
        taUser = (MLUser *)sweetheart.sponsor;
        name = taUser.nickname;
        //对方是发起人
    }
    ShareData *shareData = [ShareData shareData];
    [self.slimeView endRefresh];
     [HUD hideFromViewAnimated:YES];

    NSDictionary *dic=@{@"objectId":taUser.objectId};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"deleMySweetheartOfUserList" object:self userInfo:dic];
    
    NSLog(@"123456");
    
    if (shareData.firstView.sweetheart&&[shareData.firstView.sweetheartUser.objectId isEqualToString:taUser.objectId]) {
       
        UIButton *bt = (UIButton *)[self.view viewWithTag:kButtonTag];
        [bt setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
        shareData.firstView.sweetheart = sweetheart;
       
        [shareData.firstView assignment];
        [HUD hideFromViewAnimated:YES];
         [self changeNameButtonFrame];
        return;
    }
    if (shareData.secondView.sweetheart&&[shareData.secondView.sweetheartUser.objectId isEqualToString:taUser.objectId]) {
       
        UIButton *bt1 = (UIButton *)[self.view viewWithTag:kButtonTag+1];
        [bt1 setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
        shareData.secondView.sweetheart = sweetheart;
       
        [shareData.secondView assignment];
        [HUD hideFromViewAnimated:YES];
         [self changeNameButtonFrame];
        return;
    }
    if (shareData.thirdView.sweetheart&&[shareData.thirdView.sweetheartUser.objectId isEqualToString:taUser.objectId]) {
        
        UIButton *bt2 = (UIButton *)[self.view viewWithTag:kButtonTag+2];
        [bt2 setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
       
        shareData.thirdView.sweetheart = sweetheart;
     
        [shareData.thirdView assignment];
        [HUD hideFromViewAnimated:YES];
         [self changeNameButtonFrame];
        return;
    }
   
    if (shareData.firstView.sweetheart == nil) {
        [shareData.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [shareData.scrollView setContentSize:CGSizeMake(DeviceWidth, DeviceHeight - kMyTabBarHeight)];
        [shareData.firstView.nameBt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
       
        self.presentSweetheartCount = 1;
        UIButton *bt = (UIButton *)[self.view viewWithTag:kButtonTag];
        [bt setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
       
        shareData.firstView.sweetheart = sweetheart;
      
        [shareData.firstView assignment];
         [self changeNameButtonFrame];
    
        return;
    }
    if(shareData.secondView.sweetheart == nil){
        [shareData.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [shareData.scrollView setContentSize:CGSizeMake(DeviceWidth*2, DeviceHeight - kMyTabBarHeight)];
       
        [shareData.firstView.nameBt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
        self.presentSweetheartCount = 2;
        UIButton *bt1 = (UIButton *)[self.view viewWithTag:kButtonTag+1];
        [bt1 setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
       
        shareData.secondView.sweetheart = sweetheart;
       
        [shareData.secondView assignment];
        [self changeNameButtonFrame];
        return;
    }
    if (shareData.thirdView.sweetheart == nil) {
        self.presentSweetheartCount = 3;
        [shareData.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [shareData.scrollView setContentSize:CGSizeMake(DeviceWidth*3, DeviceHeight - kMyTabBarHeight)];
       
        [shareData.firstView.nameBt setTitleColor:APPCOLOR(1) forState:UIControlStateNormal];
        UIButton *bt2 = (UIButton *)[self.view viewWithTag:kButtonTag+2];
        [bt2 setTitle:[NSString stringWithFormat:@"%@",name] forState:UIControlStateNormal];
      
        shareData.thirdView.sweetheart = sweetheart;
       
        [shareData.thirdView assignment];
        [self changeNameButtonFrame];
        return;
    }
}
//好的体验重载所有内容,则恋人关系改变,全部重新加载
- (void)changeNameButtonFrame
{
    ShareData *sharedata = [ShareData shareData];
    switch (self.presentSweetheartCount) {
            
        case 1:
             self.moveTriangle.frame = CGRectMake(DeviceWidth/2-32/2, 0, 32, 16);
            [sharedata.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            sharedata.firstView.nameBt.hidden = NO;
             sharedata.firstView.nameBt.frame = CGRectMake(1*DeviceWidth/3, 0 , DeviceWidth/3 , kSweetheartsViewHeight);
            sharedata.secondView.nameBt.hidden = YES;
            sharedata.thirdView.nameBt.hidden = YES;
        
            break;
        case 2:
            self.moveTriangle.frame = CGRectMake(DeviceWidth/2/2-32/2, 0, 32, 16);
            [sharedata.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            sharedata.firstView.nameBt.hidden = NO;
            sharedata.firstView.nameBt.frame = CGRectMake(0, 0 , DeviceWidth/2 , kSweetheartsViewHeight);
            sharedata.secondView.nameBt.hidden = NO;
            sharedata.secondView.nameBt.frame = CGRectMake(DeviceWidth/2, 0 , DeviceWidth/2 , kSweetheartsViewHeight);
            sharedata.thirdView.nameBt.hidden = YES;
            
            break;
        case 3:
            self.moveTriangle.frame = CGRectMake(DeviceWidth/3/2-32/2, 0, 32, 16);
            [sharedata.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            
            sharedata.firstView.nameBt.hidden = NO;
            sharedata.firstView.nameBt.frame = CGRectMake(0, 0 , DeviceWidth/3 , kSweetheartsViewHeight);
            sharedata.secondView.nameBt.hidden = NO;
            sharedata.secondView.nameBt.frame = CGRectMake(1*DeviceWidth/3, 0 , DeviceWidth/3 , kSweetheartsViewHeight);
            sharedata.thirdView.nameBt.hidden = NO;
            sharedata.thirdView.nameBt.frame = CGRectMake(2*DeviceWidth/3, 0 , DeviceWidth/3 , kSweetheartsViewHeight);
            break;
            
        default:
            break;
    }
    [sharedata.firstView redHotView];
    [sharedata.secondView redHotView];
    [sharedata.thirdView redHotView];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSInteger number = 0;
    if ([ShareData shareData].firstView.sweetheart) {
        number++;
    }
    if ([ShareData shareData].secondView.sweetheart) {
        number++;
    }
    if ([ShareData shareData].thirdView.sweetheart) {
        number++;
    }
    if (number == 0) {
        return;
    }

    
    [ShareData shareData].firstView.timeView.alpha = 0;
    if([ShareData shareData].firstView){
        [ShareData shareData].firstView.chiefViewTop = YES;
        [ShareData shareData].firstView.chiefView.frame = CGRectMake(0, 0, DeviceWidth, kChiefViewHeight);
    }
    
    
     [ShareData shareData].secondView.timeView.alpha = 0;
    if([ShareData shareData].secondView){
        [ShareData shareData].secondView.chiefViewTop = YES;
         [ShareData shareData].secondView.chiefView.frame = CGRectMake(0, 0, DeviceWidth, kChiefViewHeight);
    }
     [ShareData shareData].thirdView.timeView.alpha = 0;
    if([ShareData shareData].thirdView){
       
        [ShareData shareData].thirdView.chiefViewTop = YES;
        [ShareData shareData].thirdView.chiefView.frame = CGRectMake(0, 0, DeviceWidth, kChiefViewHeight);
    }
    
    
}
//通知滑动了页面
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
     ShareData *sharedata = [ShareData shareData];
    CGFloat pagewidth = sharedata.scrollView.frame.size.width;
    int currentPage = floor(sharedata.scrollView.contentOffset.x/ pagewidth);
    [self changeScrollViewContentOffset:currentPage];
}


//滑动条滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
     ShareData *sharedata = [ShareData shareData];
    if (sharedata.scrollView.contentOffset.x != 0) {
        
        CGFloat w = DeviceWidth/self.presentSweetheartCount/2-32/2;
        
        self.moveTriangle.frame = CGRectMake(w+sharedata.scrollView.contentOffset.x/DeviceWidth*DeviceWidth/self.presentSweetheartCount, 0, 32, 16);
        
        //滑动恋人上面的指示条
    }
    if (self.slimeView) {
        [self.slimeView scrollViewDidScroll];
    }
}


- (void)enterUserDataVC
{
    MLUserDataViewController *userInfoVC = [[MLUserDataViewController alloc]init];
    userInfoVC.userData = [MLUser currentUser];
    userInfoVC.redact = YES;
    //    userInfoVC.likeUsers = self.likeUsers;
    [Utils pushTabbarHidden:userInfoVC Selfvc:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
// 根据环信id得到要显示头像路径，如果返回nil，则显示默认头像
- (NSString *)avatarWithChatter:(NSString *)chatter{
    //    return @"http://img0.bdstatic.com/img/image/shouye/jianbihua0525.jpg";
    return nil;
}

// 根据环信id得到要显示用户名，如果返回nil，则默认显示环信id
- (NSString *)nickNameWithChatter:(NSString *)chatter{
    return chatter;
}
//用户类型改变,
- (void)chatUserTypeDataChangeUser:(MLUser *)chatUser
{
    if([ShareData shareData].firstView.sweetheart){
        if ([ShareData shareData].firstView.sweetheartUser) {
            if ([[ShareData shareData].firstView.sweetheartUser.objectId isEqualToString:chatUser.objectId]) {
                [ShareData shareData].firstView.sweetheartUser = chatUser;
            }
        }
    }else if([ShareData shareData].secondView.sweetheart){
        if ([ShareData shareData].secondView.sweetheartUser) {
            if ([[ShareData shareData].secondView.sweetheartUser.objectId isEqualToString:chatUser.objectId]) {
                [ShareData shareData].secondView.sweetheartUser = chatUser;
            }
        }
    }else if([ShareData shareData].thirdView.sweetheart){
        if ([ShareData shareData].thirdView.sweetheartUser) {
            if ([[ShareData shareData].thirdView.sweetheartUser.objectId isEqualToString:chatUser.objectId]) {
                [ShareData shareData].thirdView.sweetheartUser = chatUser;
            }
        }
    }
    if ([ShareData shareData].friends) {
        //判断是否已经是好友
        for (int i = 0; i<[ShareData shareData].friends.count; i++) {
            MLUser *f = (MLUser *)[ShareData shareData].friends[i];
            if([f.objectId isEqualToString:chatUser.objectId]){
                [[ShareData shareData].friends replaceObjectAtIndex:i withObject:chatUser];
                [Utils savePassFriendObjectID:chatUser.objectId];
                break;
            }
        }
    }
    NSDictionary *dic=@{@"user":chatUser};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"chatUserTypeDataChange" object:self userInfo:dic];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addChatterUsers" object:self userInfo:dic];

}

@end
